
# 1、字典的创建
# user={"name":"tom哥","address":"杭州"}
# print(user)

# 2、通过zip() 函数将两个列表组合成字典，如果key和value长度不相同，采用最短的长度
# keys=["name","address"]
# values=["tom哥","杭州","28"]
# dict1=dict(zip(keys,values))
# print(dict1)

# 3、创建值为空的字典
# keys1=["name","address"]
# dict2=dict.fromkeys(keys1)
# print(dict2)

# 4、通过键值对访问字典
# print(dict1["name"])
# print(dict1["name_1"])

# 5、遍历字典
# dict2={"name":"tom哥","address":"杭州"}
# for item in dict2.items():
#     print(item)

# 6、添加一个元素，如果key重复，值会覆盖
# dict3={"name":"tom哥","address":"杭州","age":"19"}
# print(dict3)
# dict3["age"]=28
# print(dict3)

# 7、删除一个元素，当key不存在时，会抛异常
# dict4={"name":"tom哥","address":"杭州","age":28}
# print(dict4)
# del dict4["address"]
# print(dict4)
# if "age" in dict4:
#     del dict4["age"]
# print(dict4)

# 8、使用字典推导式快速生成一个字典
import random
dict5 = {i: random.randint(10, 11) for i in range(1, 5)}
print(dict5)
